#!/bin/bash

if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "./apache-ant-1.9.16.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

if [[ ! "$1" =~ ^/.* || ! -d "$1" || "$1" = "/" ]]; then echo "请输入正确的构建路径"; exit 1; fi
if [[ ! "$2" =~ ^/.* || ! -d "$2" || "$2" = "/" ]]; then echo "请输入正确的安装路径"; exit 1; fi
set -e
buildpath=$1
installpath=$2
if [[ "$1" =~ .*/$ ]]; then buildpath=${buildpath%/*}; fi
if [[ "$2" =~ .*/$ ]]; then installpath=${installpath%/*}; fi
[[ "$buildpath" == "$installpath" ]] && echo "构建路径与安装路径需为不同路径" && exit 1
yum install -y wget tar environment-modules 
wget -t 0 -c -P $buildpath https://dlcdn.apache.org//ant/binaries/apache-ant-1.9.16-bin.tar.gz
tar xf $buildpath/apache-ant-1.9.16-bin.tar.gz -C $buildpath && rm -rf $buildpath/apache-ant-1.9.16-bin.tar.gz

mkdir -p $installpath/apache-ant-1.9.16 && cp -r $buildpath/apache-ant-1.9.16/* $installpath/apache-ant-1.9.16/

cat>"$installpath/apache-ant-1.9.16/apache-ant-1.9.16_modulefiles"<<EOF
#%Module1.0
conflict ant
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
    variable modfile
    set modfile_path [file dirname \$modfile]
    return \$modfile_path
}
set pwd [getModulefileDir]
set ant \$pwd
setenv ant \$ant
prepend-path PATH \$ant/bin
EOF

rm -rf $buildpath/apache-ant-1.9.16

